package com.binvshe.binvshe.contacts;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.view.CommentDialog;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.GlideHelper;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 搜索结果
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-10-27
 * @Version: 1.0.0
 */
public class SearchResultAdapter extends RecyclerViewBaseAdapter {

    private int flag;

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public SearchResultAdapter(Context con) {
        super(con);
    }


    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(setAdapterViewItem(parent, R.layout.list_item_search_result));
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final People people = (People) getData().get(position);

        viewHolder.tvName.setText(people.getNick_name());
        viewHolder.tvContent.setText(people.getSign());
        GlideHelper.loadImageWithRound(getContext(), viewHolder.ivPortraid, people.getPortrait());

        if (flag == SearchFriendActivity.FLAG_ADD_FRIEND) {// 添加好友
            viewHolder.button.setVisibility(View.VISIBLE);
            viewHolder.button.setText("加好友");
            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (getContext() instanceof AppCompatActivity) {
                        AppCompatActivity activity = (AppCompatActivity) getContext();
                      /*  final EditDialogFragment reasonDialog = new EditDialogFragment();
                        reasonDialog.setOnCententSavedListener(new EditDialogFragment.OnContentSavedListener() {
                            @Override
                            public void OnContentSaved(final CharSequence content) {
                                requestAddFriend("" + people.getUser_id(), content.toString());
                                reasonDialog.dismiss();
                            }
                        })
                                .setTitle("请输入申请理由")
                                .show(activity.getSupportFragmentManager());
*/

                        final CommentDialog commentDialog = CommentDialog.newInstance("申请理由", "请输入申请理由", 4, true);
                        commentDialog.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), "dd");
                        commentDialog.setOnClickable(new CommentDialog.CommentClickable() {

                            @Override
                            public void btnEnter(final String comment) {
                                requestAddFriend("" + people.getUser_id(), comment);
                                commentDialog.dismiss();
                            }
                        });

                    }

                }
            });

            viewHolder.tvContent.setVisibility(View.GONE);

        } else { // 搜索我的好友

        }

    }


    void requestAddFriend(String userId, String content) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("target_id", userId);
            jsonObject.put("reason", content);
            IHttpRequest request = new MyHttpRequest(getContext());
            request.post(UrlCenter.REQEST_ADD_FRIEND, jsonObject, new DataRequestListener<Object>(Object.class) {

                @Override
                public void onSuccess(Object acct) {
                    super.onSuccess(acct);
                    EasyToast.showToast(getContext(), "申请成功，请等待");
                    notifyDataSetChanged();
                }

                @Override
                public void onFailure(Throwable t, int errorNo, String strMsg) {
                    super.onFailure(t, errorNo, strMsg);
                    EasyToast.showToast(getContext(), strMsg);
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.ivPortraid)
        public ImageView ivPortraid;
        @Bind(R.id.tvName)
        public TextView tvName;
        @Bind(R.id.tvContent)
        public TextView tvContent;
        @Bind(R.id.button)
        public Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
